// JavaScript Document
$(function() {
    var testEditor;
    testEditor = editormd('editormd', {
        width: '100%',
        height: 680,
        // theme: 'dark', //工具栏主题
        // editorTheme: 'pastel-on-dark', // 编辑主题
        // previewTheme: 'dark', // 预览主题
        syncScrolling: 'single',
        path: '/static/editor/lib/', // lib的路径
        saveHTMLToTextarea: true, // 这个配置在simple.html中并没有，但是为了能够提交表单，使用这个配置可以让构造出来的HTML代码直接在第二个隐藏的textarea域中，方便post提交表单。
        emoji: true,
        taskList: true,
        tocm: true, // Using [TOCM]
        imageUpload: true, // 开启图片上传功能
        imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
        imageUploadURL: "",
        toolbarIcons : function() {
            // return ["undo","redo","|","bold","del","italic","quote","ucwords","uppercase","lowercase","|","h1","h2","h3","h4","h5","h6","|","list-ul","list-ol","hr","|","link","reference-link","image","code","preformatted-text","code-block","table","datetime","emoji","html-entities","pagebreak","|","goto-line","watch","preview","fullscreen","clear","search","|","help","info"]
            return ["undo","redo","|","bold","italic","h2","del","|","list-ul","list-ol","hr","|","quote","link","image","code","preformatted-text","table","emoji","|","watch","search","save","help",]
        },
        // 自定义工具栏按钮的事件处理
        toolbarHandlers: {
            save: function(){
                $.ajax({
                    url:'/user/article/save',
                    method:'post',
                    contentType:'application/x-www-form-urlencoded',
                    data: $('#form-article').serialize(),
                    dataType: 'json',
                    success: function(data){
                        layer.msg(data.resultMsg);
                        setTimeout(function(){
                            window.location = '/user/markdown/' + data.art_id;
                        }, 1000);
                    },
                    error:function(){
                        layer.msg("回调操作异常");
                    }
                });

            }
        },
    });
});

/**
 * 触发‘发布文章’按钮
 */
$(function(){
    $('#article-submit').on('click', function(){
        $(this).attr('disabled', 'disabled');
        $.ajax({
            url: '/user/article/publish',
            method: 'post',
            contentType:'application/x-www-form-urlencoded;charset=utf-8',
            data: $('#form-article').serialize(),
            dataType: 'json',
            success: function(data){
                layer.msg(data.resultMsg);
                setTimeout(function(){
                    window.location = '/user/index';
                }, 1000);
            },
            error: function(){
                layer.msg("回调操作异常");
            }
        });
    })
});


/**
 * 退出当前编辑页面
 */
function exitPage() {
    var boolean = confirm('退出后将不可撤销');
    if(boolean)
        window.location = '/user/manage/index';
    else
        return;
}

